-----------------------------------------------------------------------------------------
--
-- game.lua
-- Background graphic from http://opengameart.org/content/starfield-background, courtesy of Sauer2
-- Monkey, enemy, and bullet graphics are from http://www.vickiwenderlich.com/2013/05/free-game-art-space-monkey/
--
-----------------------------------------------------------------------------------------

-- Require in some of Corona's 
local movieclip = require('movieclip')
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

-- include Corona's "physics" library
local physics = require "physics"
physics.start(); physics.pause()

local numberOfLives = 3
local bulletSpeed = 0.35
local badGuyMovementSpeed = 1500
local badGuyCreationSpeed = 800

-- forward declarations and other locals
local background, tmr_createBadGuy, monkey, bullet, txt_score
local tmr_createBadGuy
local lives = {}
local badGuy = {}
local badGuyCounter = 1
local score = 0
local shot = audio.loadSound("sounds/shot.wav")
local shoted = audio.loadSound("sounds/shoted.wav")
local bgsound = audio.loadSound( "sounds/jungle.mp3" )
local hit
-- Called when the scene's view does not exist:
function scene:createScene( event )
	local group = self.view	
	audio.play(bgsound)
	
	function touched(event)		

		if(event.phase == "began") then
			audio.play(shot)
			angle = math.deg(math.atan2((event.y-monkey.y),(event.x-monkey.x)))            
            monkey.rotation = angle + 90

		--	bullet = display.newImageRect("images/grenade_red.png",12,16)
			bullet =movieclip.newAnim({'images/monster_1.png','images/monster_2.png','images/monster_3.png','images/monster_4.png','images/monster_5.png','images/monster_6.png'})
				bullet.x = halfW
				bullet.y = halfH				
				bullet.name = "bullet"
				physics.addBody( bullet, "dynamic", { isSensor=true, radius=screenH*.025} )
				group:insert(bullet)
			bullet:play()

			local farX = screenW*2
			local slope = ((event.yStart-screenH/2)/(event.xStart-screenW/2))			
			local yInt = event.yStart - (slope*event.xStart)

			if(event.xStart >= screenW/2)then
				farX = screenW*2
			else
				farX = screenW-(screenW*2)
			end

			local farY = (slope*farX)+yInt
			
			local xfactor = farX-bullet.x
			local yfactor = farY-bullet.y
			
			local distance = math.sqrt((xfactor*xfactor) + (yfactor*yfactor))

			bullet.trans = transition.to(bullet, { time=distance/bulletSpeed, y=farY, x=farX, onComplete=nil})
		end
	end

	-- Create a background for our game
	background = display.newImageRect("images/background_3.png", 480, 320)
		background.x = halfW
		background.y = halfH
		group:insert(background)
	
	-- Place our monkey in the center of screen

	



	local circle = display.newImageRect("images/circle.png",100,100)
		circle.x = halfW
		circle.y = halfH
		group:insert(circle)
	
	local function updateCLock(e)  
    	circle.rotation = - system.getTimer()/25;      
	end 
	timer.performWithDelay(1, updateCLock, 1000)

--	monkey = display.newImageRect("images/ladybug_2.png",40,40)
	monkey = movieclip.newAnim({'images/ladybug_1.png', 'images/ladybug_1.png', 'images/ladybug_2.png', 'images/ladybug_2.png', 'images/ladybug_3.png', 'images/ladybug_3.png', 'images/ladybug_4.png', 'images/ladybug_4.png', 'images/ladybug_5.png', 'images/ladybug_5.png', 'images/ladybug_6.png', 'images/ladybug_6.png'})
		monkey.x = halfW
		monkey.y = halfH
		group:insert(monkey)
	monkey:play()

	txt_score = display.newText("Score: "..score,20,20,native.systemFont,22)
		txt_score.x = 430
		group:insert(txt_score)
	
	-- Insert our lives, but show them as bananas
	for i=1,numberOfLives do
		lives[i] = display.newImageRect("images/heart.png",45,34)
			lives[i].x = i*40-20
			lives[i].y = 18
			group:insert(lives[i])
	end

	-- This function will create our bad guy
	function createBadGuy()		

		-- Determine the enemies starting position
		local startingPosition = math.random(1,4)
		display.remove( hit )
		if(startingPosition == 1) then
			-- Send bad guy from left side of the screen
			startingX = -10
			startingY = math.random(0,screenH)
		elseif(startingPosition == 2) then
			-- Send bad guy from right side of the screen
			startingX = screenW + 10
			startingY = math.random(0,screenH)
		elseif(startingPosition == 3) then
			-- Send bad guy from the top of the screen
			startingX = math.random(0,screenW)
			startingY = -10
		else
			-- Send bad guy from the bototm of the screen
			startingX = math.random(0,screenW)
			startingY = screenH + 10
		end

		-- Start the bad guy according to starting position
		badGuy[badGuyCounter] = display.newImageRect("images/virus2_2.png",34,34)
			badGuy[badGuyCounter].x = startingX
			badGuy[badGuyCounter].y = startingY
			physics.addBody( badGuy[badGuyCounter], "dynamic", { isSensor=true, radius=17} )
			badGuy[badGuyCounter].name = "badGuy"
			group:insert(badGuy[badGuyCounter])

		-- Then move the bad guy towards the center of the screen. Once the transition is done, remove the bad guy.
		
		badGuy[badGuyCounter].trans = transition.to(badGuy[badGuyCounter], { time=badGuyMovementSpeed, x=halfW, y=halfH, 			
		onComplete = function (self)
			audio.play(shoted)	
			
			-- hit = display.newImageRect("images/ladybug_hit.png",50,50)
			-- hit.x = halfW
			-- hit.y = halfH
			-- group:insert(hit)

			
			hit =movieclip.newAnim({'images/ladybug_hit_1.png','images/ladybug_hit_2.png','images/ladybug_hit_3.png','images/ladybug_hit_4.png','images/ladybug_hit_5.png'})
				hit.x = halfW
				hit.y = halfH				
				hit.name = "hit"
				physics.addBody( hit, "static", { isSensor=true, radius=screenH*.025} )
				group:insert(hit)
			hit:play()

			self.parent:remove(self); 
			self = nil;

			-- Since the bad guy has reached the monkey, we will want to remove a banana
			display.remove(lives[numberOfLives])
			numberOfLives = numberOfLives - 1			

			-- If the numbers of lives reaches 0 or less, it's game over!
			if(numberOfLives <= 0) then
				display.remove(monkey)
				timer.cancel(tmr_createBadGuy)
				background:removeEventListener("touch", touched)

				audio.pause( bgsound )

				local txt_gameover = display.newText("Game Over!",0,0,native.systemFont,32)
					txt_gameover.x = halfW
					txt_gameover.y = screenH * 0.3
					group:insert(txt_gameover)

				local function onGameOverTouch(event)
					if(event.phase == "began") then
						storyboard.gotoScene("menu")
					end
				end

				local txt_gameover = display.newText("Return To Menu",0,0,native.systemFont,32)
					txt_gameover.x = halfW
					txt_gameover.y = screenH * 0.7
					txt_gameover:addEventListener("touch",onGameOverTouch)
					group:insert(txt_gameover)


				display.remove( hit )
			end
		end; })

		badGuyCounter = badGuyCounter + 1
	end

	-- This function handles the collisions. In our game, this will remove the bullet and bad guys when they collide. 
	function onCollision( event )		
		if(event.object1.name == "badGuy" and event.object2.name == "bullet" or event.object1.name == "bullet" and event.object2.name == "badGuy") then			
			audio.play(shoted)
			-- Update the score
			score = score + 1
			txt_score.text = "Score: "..score
			badGuyCreationSpeed = badGuyCreationSpeed -50
			badGuyMovementSpeed = badGuyMovementSpeed -20
			

			-- Make the objects invisible
			--event.object1.alpha = 0
			--event.object2.alpha = 5	

			
			
			-- Cancel the transitions on the object
			transition.cancel(event.object1.trans)
			transition.cancel(event.object2.trans)			

			-- Then remove the object after 1 cycle. Never remove display objects in the middle of collision detection. 
			local function removeObjects()	
				if (event.object1.name=="bullet") then			
					display.remove(event.object1)
				end
				if (event.object2.name=="bullet") then
					display.remove(event.object2)
				end
			end
			timer.performWithDelay(1, removeObjects, 1)
		end
	end
end

function scene:enterScene( event )
	local group = self.view

	-- Actually start the game!	
	physics.start()
	
	tmr_createBadGuy = timer.performWithDelay(badGuyCreationSpeed, createBadGuy, 0)
	background:addEventListener("touch", touched)
	Runtime:addEventListener( "collision", onCollision )
end

scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )

return scene